package com.csaba.account;

import java.net.URL;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.forms.FormColors;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

public class AccountPlugin extends AbstractUIPlugin
{
	// The shared instance.
	private static AccountPlugin plugin;
	// Resource bundle.
	private ResourceBundle resourceBundle;
	private FormColors formColors;
	public static final String IMG_FORM_BG = "formBg"; //$NON-NLS-1$
	public static final String IMG_FHB = "fhbLogo"; //$NON-NLS-1$
	public static final String IMG_HVB = "hvbLogo"; //$NON-NLS-1$
	public static final String IMG_REIFFEISEN = "reiffeisenLogo"; //$NON-NLS-1$
	public static final String IMG_C = "typeC"; //$NON-NLS-1$
	public static final String IMG_E = "typeE"; //$NON-NLS-1$
	public static final String IMG_I = "typeI"; //$NON-NLS-1$

	/**
	 * The constructor.
	 */
	public AccountPlugin()
	{
		plugin = this;
		try
		{
			resourceBundle = ResourceBundle.getBundle("org.eclipse.ui.forms.examples.internal.ExamplesPluginResources"); //$NON-NLS-1$
		}
		catch ( final MissingResourceException x )
		{
			resourceBundle = null;
		}
	}

	@Override
	protected void initializeImageRegistry( final ImageRegistry registry )
	{
		registerImage(registry, IMG_FORM_BG, "form_banner.gif"); //$NON-NLS-1$
		registerImage(registry, IMG_FHB, "fhb.png"); //$NON-NLS-1$
		registerImage(registry, IMG_HVB, "hvb.png"); //$NON-NLS-1$
		registerImage(registry, IMG_REIFFEISEN, "raiffeisen.png"); //$NON-NLS-1$
		registerImage(registry, IMG_C, "c.png"); //$NON-NLS-1$
		registerImage(registry, IMG_E, "e.png"); //$NON-NLS-1$
		registerImage(registry, IMG_I, "i.png"); //$NON-NLS-1$
	}

	private void registerImage( final ImageRegistry registry, final String key, final String fileName )
	{
		try
		{
			final IPath path = new Path("icons/" + fileName); //$NON-NLS-1$
			final URL url = FileLocator.find(getBundle(), path, null);
			if ( url != null )
			{
				final ImageDescriptor desc = ImageDescriptor.createFromURL(url);
				registry.put(key, desc);
			}
		}
		catch ( final Exception e )
		{
		}
	}

	public FormColors getFormColors( final Display display )
	{
		if ( formColors == null )
		{
			formColors = new FormColors(display);
			formColors.markShared();
		}
		return formColors;
	}

	/**
	 * Returns the shared instance.
	 */
	public static AccountPlugin getDefault()
	{
		return plugin;
	}

	/**
	 * Returns the string from the plugin's resource bundle, or 'key' if not
	 * found.
	 */
	public static String getResourceString( final String key )
	{
		final ResourceBundle bundle = AccountPlugin.getDefault().getResourceBundle();
		try
		{
			return ( bundle != null ? bundle.getString(key) : key );
		}
		catch ( final MissingResourceException e )
		{
			return key;
		}
	}

	/**
	 * Returns the plugin's resource bundle,
	 */
	public ResourceBundle getResourceBundle()
	{
		return resourceBundle;
	}

	@Override
	public void stop( final BundleContext context ) throws Exception
	{
		try
		{
			if ( formColors != null )
			{
				formColors.dispose();
				formColors = null;
			}
		}
		finally
		{
			super.stop(context);
		}
	}

	public Image getImage( final String key )
	{
		return getImageRegistry().get(key);
	}

	public ImageDescriptor getImageDescriptor( final String key )
	{
		return getImageRegistry().getDescriptor(key);
	}
}
